package com.example.epidemic_assistant.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.epidemic_assistant.R;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 去过的地址适配器
 */
public class PlaceAdapter extends RecyclerView.Adapter<PlaceAdapter.PlaceViewHolder> {

    private static final String TAG = "PlaceAdapter";

    private Context mContext;
    private List<String> mPlaceList;
    private IDeleteClick mIDeleteClick;

    public PlaceAdapter(Context context, List<String> placeList) {
        mContext = context;
        mPlaceList = placeList;
    }

    @NotNull
    @Override
    public PlaceViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        return new PlaceViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_place_people, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull PlaceViewHolder holder, int position) {
        String info = mPlaceList.get(position);
        holder.mTvInfo.setText(info);
        holder.mBtnDelete.setOnClickListener(view -> {
            mIDeleteClick.onDeleteClick(position);
        });
    }

    @Override
    public int getItemCount() {
        if (mPlaceList == null) {
            return 0;
        } else {
            return mPlaceList.size();
        }
    }

    public class PlaceViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTvInfo;
        private final Button mBtnDelete;

        public PlaceViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);

            mTvInfo = itemView.findViewById(R.id.tv_info);
            mBtnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }

    public void setOnDeleteClick(IDeleteClick iDeleteClick) {
        mIDeleteClick = iDeleteClick;
    }

    public interface IDeleteClick {
        void onDeleteClick(int position);
    }
}
